package com.label.Web;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.ImmutableMap;
import com.label.Config.Logger.LogAnnotation;
import com.label.Model.Common.ResponseBean;
import com.label.Model.Common.Validation.Del;
import com.label.Model.Common.Validation.Get;
import com.label.Model.Common.Validation.Save;
import com.label.Model.Common.Validation.Update;
import com.label.Model.Do.Persons;
import com.label.Model.Query.MessageQuery;
import com.label.Model.Query.PersonsQuery;
import com.label.Model.Query.ProcessQuery;
import com.label.Service.PersonsService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@RestController
@RequestMapping(value = "/persons")
public class PersonsController {

    private final static Logger logger = LoggerFactory.getLogger(PersonsController.class);

    @Resource
    public PersonsService personsService;


    @RequestMapping("/getAll")
    @LogAnnotation(module = "分区模块",operator = "获取所有分群")
    @RequiresAuthentication
    public ResponseBean getAll() {
        try {
            return new ResponseBean(200,"成功",personsService.getAll());
        } catch (Exception e) {
            logger.info(e.getMessage());
            return new ResponseBean(400,"未知错误",null);
        }
    }

    @RequestMapping("/getOne")
    @LogAnnotation(module = "分区模块",operator = "获取一个分群")
    @RequiresAuthentication
    public ResponseBean getOne(@Validated(value = Get.class) @RequestBody PersonsQuery personsQuery) {
        try {
            return new ResponseBean(200,"成功",personsService.getOne(BeanUtil.copyProperties(personsQuery, Persons.class)));
        } catch (Exception e) {
            return new ResponseBean(400,"未知错误",null);
        }
    }

    @RequestMapping("/getMyPersons")
    @LogAnnotation(module = "分区模块",operator = "获取我的画像")
    @RequiresAuthentication
    public ResponseBean getMyPersons() {
        try {
            return new ResponseBean(200,"成功",personsService.getMy());
        } catch (Exception e) {
            return new ResponseBean(400,"未知错误",null);
        }
    }

    @RequestMapping("/getMyPersonsByTheme")
    @LogAnnotation(module = "分区模块",operator = "获取主题下我的画像")
    @RequiresAuthentication
    public ResponseBean getMyPersonsByTheme(@RequestBody PersonsQuery personsQuery) {
        try {
            return new ResponseBean(200,"成功",personsService.getMyByTheme(personsQuery.getTheme()));
        } catch (Exception e) {
            return new ResponseBean(400,"未知错误",null);
        }
    }

    @RequestMapping("/deletePersonsById")
    @LogAnnotation(module = "分区模块",operator = "假式删除")
    @RequiresAuthentication
    public ResponseBean deletePersonsPersonsById(@Validated(value = Del.class) @RequestBody PersonsQuery personsQuery) {
        try {
            if(personsService.deletePersonsById(personsQuery.getId()))
                return new ResponseBean(HttpStatus.OK.value(), "成功!",true);
            return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "画像已删除！",true);
        }  catch (Exception e) {
            e.printStackTrace();
            logger.error("传入参数：" + personsQuery + e.getMessage());
            return new ResponseBean(400,"未知错误，删除失败！",null);
        }
    }

    @RequestMapping("/deletePersonsByIdReal")
    @LogAnnotation(module = "分区模块",operator = "物理删除")
    @RequiresAuthentication
    public ResponseBean deletePersonsPersonsByIdReal(@Validated(value = Del.class) @RequestBody PersonsQuery personsQuery) {
        try {
            if(personsService.deletePersonsById(personsQuery.getId()))
                return new ResponseBean(HttpStatus.OK.value(), "成功!",personsService.deletePersonsByIdReal(personsQuery.getId()));
            return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "画像不存在或存在绑定信息！",true);
        }  catch (Exception e) {
            e.printStackTrace();
            logger.error("传入参数：" + personsQuery + "/n" + e.getMessage());
            return new ResponseBean(400,"未知错误",null);
        }
    }

    @RequestMapping("/addPersons")
    @LogAnnotation(module = "分区模块",operator = "添加画像")
    @RequiresAuthentication
    public ResponseBean addPersons(@RequestBody PersonsQuery personsQuery) {
        try {
            Persons persons=BeanUtil.copyProperties(personsQuery, Persons.class);
            Long id=personsService.addPersons(persons);
            if(id!=null)
                return new ResponseBean(200,"成功", ImmutableMap.of("id",id.toString()));
            return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "画像名重复！",true);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("传入参数：" + personsQuery + "/n" + e.getMessage());
            return new ResponseBean(400,"未知错误！",null);
        }
    }

    @RequestMapping("/updatePersons")
    @LogAnnotation(module = "分区模块",operator = "更新画像")
    @RequiresAuthentication
    public ResponseBean updatePersons(@Validated(value = Update.class) @RequestBody PersonsQuery personsQuery) {
        try {
            if(personsService.updatePersonsById(BeanUtil.copyProperties(personsQuery, Persons.class)))
                return new ResponseBean(200,"成功",true);
            return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "画像不存在！",true);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("传入参数：" + personsQuery + "/n" + e.getMessage());
            return new ResponseBean(400,"未知错误！",null);
        }
    }

    @RequestMapping("/resumePersons")
    @LogAnnotation(module = "分区模块",operator = "恢复假式删除")
    @RequiresAuthentication
    public ResponseBean resumePersons(@Validated(value = Update.class) @RequestBody PersonsQuery personsQuery) {
        try {
            if(personsService.resumePersonsById(personsQuery.getId()))
                return new ResponseBean(200,"成功",true);
            return new ResponseBean(HttpStatus.INTERNAL_SERVER_ERROR.value(), "画像已存在！",true);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("传入参数：" + personsQuery + "/n" + e.getMessage());
            return new ResponseBean(400,"未知错误！",null);
        }
    }

    @RequestMapping("/personDataProcess")
    @LogAnnotation(module = "分区模块",operator = "数据计算")
    @RequiresAuthentication
    public ResponseBean personDataProcess(@RequestBody ProcessQuery processQuery ) {
        try {
            Boolean flag=personsService.savePersonasData(processQuery);
            if(flag)
                return new ResponseBean(200,"成功",true);
            return new ResponseBean(400,"参数错误",false);
        } catch (Exception e) {
            logger.error("传入参数2：" + processQuery + e.getMessage());
            return new ResponseBean(400,"未知错误",null);
        }
    }

    @RequestMapping("/themePersonDataProcess")
    @LogAnnotation(module = "分区模块",operator = "主题数据计算")
    @RequiresAuthentication
    public ResponseBean themePersonDataProcess(@Validated(value = Save.class) @RequestBody ProcessQuery processQuery ) {
        try {
            personsService.dataProcess(processQuery.getRealId(), processQuery.getSql(), processQuery.getTheme());
            return new ResponseBean(200,"成功",true);
        } catch (Exception e) {
            logger.error("传入参数2：" + processQuery + e.getMessage());
            return new ResponseBean(400,"未知错误",null);
        }
    }

    @RequestMapping("/deletePersonDataProcess")
    @LogAnnotation(module = "分区模块",operator = "删除数据计算结果")
    @RequiresAuthentication
    public ResponseBean deletePersonDataProcess(@RequestBody ProcessQuery processQuery) {
        try {
            return new ResponseBean(200,"成功",personsService.deletePersonasData(processQuery.getRealId()));
        } catch (Exception e) {
            logger.error("传入参数：" + "/n" + e.getMessage());
            return new ResponseBean(400,"未知错误",null);
        }
    }

    @RequestMapping("/checkName")
    @LogAnnotation(module = "分区模块",operator = "检查名字是否重复")
    @RequiresAuthentication
    public ResponseBean checkPersonsName(@RequestBody PersonsQuery personsQuery) {
        try{
            if(personsQuery.getName()!=null&&!StringUtils.isBlank(personsQuery.getName()))
                return new ResponseBean(200,"成功",personsService.cheakPersonsName(personsQuery.getName()));
            return new ResponseBean(400,"参数错误",null);
        } catch (Exception e) {
            logger.error("传入参数：" + personsQuery + "/n" + e.getMessage());
            return new ResponseBean(400,"未知错误",null);
        }
    }

    @RequestMapping("/sendMessage")
    @LogAnnotation(module = "分区模块",operator = "发送家校信息")
    @RequiresAuthentication
    public ResponseBean sendMessage(@Validated @RequestBody MessageQuery messageQuery) {
        try{
            if(personsService.sendMessage(messageQuery))
                return new ResponseBean(200,"成功",true);
            return new ResponseBean(400,"该用户邮箱不存在",false);
        } catch (Exception e) {
            logger.error("传入参数：" + messageQuery + "/n" + e.getMessage());
            return new ResponseBean(400,"未知错误",null);
        }
    }

    @RequestMapping("/sendMessage2")
    @RequiresAuthentication
    public ResponseBean sendMessage2(@RequestParam("jsonData") String post,@RequestParam("file") MultipartFile file) {
        MessageQuery messageQuery=JSONObject.parseObject(post,MessageQuery.class);
        try{
            if(personsService.sendMessage2(messageQuery,file))
                return new ResponseBean(200,"成功",true);
            return new ResponseBean(400,"该用户邮箱不存在",false);
        } catch (Exception e) {
            logger.error("传入参数：" + messageQuery + "/n" + e.getMessage());
            return new ResponseBean(400,"未知错误",null);
        }
    }

    @RequestMapping("/sendMessage3")
    @RequiresAuthentication
    public ResponseBean sendMessage3(MessageQuery messageQuery,@RequestParam("file") MultipartFile file) {
        try{
            if(personsService.sendMessage2(messageQuery,file))
                return new ResponseBean(200,"成功",true);
            return new ResponseBean(400,"该用户邮箱不存在",false);
        } catch (Exception e) {
            logger.error("传入参数：" + messageQuery + "/n" + e.getMessage());
            return new ResponseBean(400,"未知错误",null);
        }
    }
}
